<template>
	<basic-container>
		<avue-crud :option="option" :data="data" :page="page" ref="crud" @search-change="searchChange" @search-reset="searchReset"
		 @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" @row-update="rowUpdate" @on-load="onLoad"
		 :table-loading="loading" @selection-change="selectionChange">
			<template slot="menuLeft">
				<el-button size="mini" type="warning" :loading="btnload" @click="audit">审核</el-button>
			</template>
			<template slot-scope="{row}" slot="cfmQty">
				<el-input-number size="mini" v-if="row.status == 2" style="width: 100px;" v-model="row.cfmQty" :min="0"
				 controls-position="right"></el-input-number>
				<span v-if="row.status != 2">{{row.cfmQty}}</span>
			</template>
			<template slot-scope="{row}" slot="checkQty">
				<span style="color: red;" v-if="row.quantity != row.checkQty">{{row.checkQty}}</span>
				<span v-if="row.quantity == row.checkQty">{{row.checkQty}}</span>
			</template>
			<template slot="menuRight">
				<el-radio-group @change="searchReset" v-model="status" size="mini" style="margin-right: 50px;">
					<el-radio-button label="">所有</el-radio-button>
					<el-radio-button :label="item.value" v-for="(item,i) in statusItem" :key="i">{{item.label}}</el-radio-button>
				</el-radio-group>
			</template>
			<template slot-scope="{row}" slot="status">
				<el-tag v-for="(item,i) in statusItem" size="mini" :key="i" :type="item.type" v-if="item.value == row.status">{{item.label}}</el-tag>
			</template>
		</avue-crud>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';

	export default {
		data() {
			return {
				status: "",
				statusItem: [{
					value: 0,
					type: 'info',
					label: '暂存'
				}, {
					value: 1,
					type: '',
					label: '已提交'
				}, {
					value: 2,
					type: 'warning',
					label: '已盘点'
				}, {
					value: 7,
					type: 'success',
					label: '已审核'
				}],
				propList: [],
				btnload: false,
				loading: false,
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					selection: true,
					searchMenuSpan: 4,
					border: true,
					index: true,
					addBtn: false,
					stripe: true,
					editBtn: false,
					refreshBtn: false,
					excelBtn: true,
					menuWidth: '80px',
					showSummary: true,
					sumColumnList: [{
						name: 'quantity',
						type: 'sum'
					}, {
						name: 'checkQty',
						type: 'sum'
					}, {
						name: 'cfmQty',
						type: 'sum'
					}],
					column: [{
							label: "盘点单号",
							prop: "checkCode",
							display: false,
							searchSpan: 4,
							search: true,
							width: 150
						}, {
							label: "库位编号",
							prop: "localCode",
							searchSpan: 4,
							search: true
						},
						{
							label: "条码",
							prop: "barCode",
							searchSpan: 4,
							search: true
						},
						{
							label: "库存数",
							prop: "quantity",
							type: "number",
							align:'right',
							width: 80
						},
						{
							label: "盘点数",
							prop: "checkQty",
							slot: true,
							align:'right',
							type: "number",
							width: 80
						},
						{
							label: "审核数",
							prop: "cfmQty",
							slot: true,
							align:'right',
							type: "number",
							width: 130
						},
						{
							label: "状态",
							prop: "status",
							slot: true,
							width: 80
						},
						{
							label: "审核时间",
							prop: "auditedOn",
							width: 160
						},
						{
							label: "审核人",
							prop: "auditedBy",
							width: 100
						}
					]
				},
				data: []
			};
		},
		created() {},
		methods: {
			selectionChange(list) {
				this.propList = [];
				list.forEach(e => {
					if (e.status == 2) {
						this.propList.push(e)
					}
				})
			},
			audit() {
				this.$confirm("确认操作?", {
						type: "warning"
					})
					.then(() => {
						this.btnload = true;
						request({
							url: '2/api/WmsCheckDetail/audits',
							method: 'post',
							data: this.propList
						}).then(res => {
							this.btnload = false;
							if (res.data.code == 200) {
								this.onLoad(this.page);
							}
						});
					}).catch(() => {});
			},
			rowUpdate(row, index, loading) {
				return request({
					url: '2/api/WmsCheckDetail/edit',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.page);
				});
			},
			rowDel(row) {
				this.$confirm("确定数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return request({
							url: '2/api/WmsCheckDetail/remove',
							method: 'post',
							params: {
								ids: row.id
							}
						});
					})
					.then(() => {
						this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				this.loading = true;
				request({
					url: '2/api/WmsCheckDetail/getpages',
					method: 'get',
					params: {
						key: this.query.barCode == undefined ? '' : this.query.barCode,
						type: this.query.checkCode == undefined ? '' : this.query.checkCode,
						status: this.status,
						where: this.query.localCode == undefined ? '' : this.query.localCode,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.data.forEach(m=>{
						if (m.status == 2 && m.cfmQty==0){
							m.cfmQty = m.checkQty
						}
					})
					this.page.total = data.remark;
					this.loading = false;
				});
			}
		}
	};
</script>

<style>
</style>
